#include <iostream>
#include  <stdlib.h>


using namespace std;

struct DoList
{
    int date;
    struct DoList *last;
    struct DoList *next;
};
DoList createTable(DoList *top);
DoList printfTable(DoList *top);
DoList findouTable(DoList *top);
DoList insertTable(DoList *top);
DoList deleteTable(DoList *top);

DoList createTable(DoList *top)
{
    cout<<"please input number for new table(Exit press any character)"<<endl;
    int num;
    bool bContinue = true;
    struct DoList *p,*t;
    top = (DoList *)malloc(sizeof(DoList));
    (*top).next = NULL;
    (*top).last = NULL;
    p = top;
    while (bContinue)
    {
        cin >> num;
//        if (cin.good())
//        {
//            t =  (DoList *)malloc(sizeof(DoList));
//            (*p).next = t;
//            (*t).last = p;
//            (*t).date = num;
//            (*t).next = NULL;
//            p = t;
//        }
//        else
//        {
//            cin.clear();
//            cin.ignore();
//            bContinue = false;
//        }
        if(!cin.good())
        {
            cin.clear();
            cin.ignore();
            bContinue = false;
            break;
        }
        t =  (DoList *)malloc(sizeof(DoList));
        (*p).next = t;
        (*t).last = p;
        (*t).date = num;
        (*t).next = NULL;
        p = t;
    }
    printfTable(top);
    return *top;
}//建立一个新的链表
DoList printfTable(DoList *top)
{
    cout<<"the new table is "<<endl;
    struct DoList *p;
	p=(*top).next;
	while (p)
	{
		cout<<((*p).date)<<endl;
		p=(*p).next;
	}
    return *top;
}//输出链表
DoList findouTable(DoList *top)
{
	int num;
	int i = 1;
	DoList *p;
	p = (*top).next;
	cout<<"please enter you want find number with "<<endl;
	cin>>num;
	while (p&&(*p).date!=num)
	{
		p=(*p).next;
		i++;
	}
	cout<<"the number "<<num<<" position in "<<i<<endl;
	printfTable(top);
    return *top;
}//查找链表中的数据的位置
DoList insertTable(DoList *top)
{
	int i,num,po;
	DoList *p,*t;
	cout<<"please input you want insert number"<<endl;
	cin>>num;
	cout<<"please input you want insert position"<<endl;
	cin>>po;
	p = top;
	for (i=1;i<po;i++)
	{
        p = (*p).next;
    }
    t = (DoList *)malloc(sizeof(DoList));
    (*t).date = num;
    (*t).next = (*p).next;
    ((*p).next)->last = t;
    (*t).last = p;
    (*p).next = t;
	printfTable(top);
	return *top;
}//在链表中插入一个数据
DoList deleteTable(DoList *top)
{
    int num;
    DoList *p,*t;
    p = (*top).next;
	cout<<"please input you want delete number"<<endl;
	cin>>num;
    while((*p).date != num)
    {
        t = p;
        p = (*p).next;
    }
    (*t).next = (*p).next;
    printfTable(top);
    return *top;
}//删除链表中的数据
int main()
{
    int r=1;
    bool toD = true;
    struct DoList *top;
    top = (DoList *)malloc(sizeof(DoList));
    while(toD)
    {
        cout<<"1,input data create a new table"<<endl;
        cout<<"2, print the table"<<endl;
        cout<<"3, find digital in the table"<<endl;
        cout<<"4, insert digital in the table"<<endl;
        cout<<"5, delete digital in the table"<<endl;
        cout<<"6, select exit"<<endl;
        cin.clear();
        cin>>r;
        switch (r)
        {
            case 1:
                createTable(top);
                break;
            case 2:
                printfTable(top);
                break;
            case 3:
                findouTable(top);
                break;
            case 4:
                insertTable(top);
                break;
            case 5:
                deleteTable(top);
                break;
            case 6:
                toD = false;
                break;
            default:break;
        }
    }
    return 0;
}






